
import { app, BrowserWindow, session, ipcMain, dialog } from "electron";
import log from 'electron-log/main';
import path from "path";
const ffmpegPath = path.join(__dirname, process.env.npm_lifecycle_event == 'dev' ? "../cdn/ffmpeg.exe" : "../../cdn/ffmpeg.exe");
let win: any = null;
import mysql from './mysql';
import capture from './capture';
import deepseek from './deepseek';
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const csv = require('csv-parser');
import './baidu';
import './tts';
// 当启动项目遇到Electron Security Warning (Insecure Content-Security-Policy)警告信息时
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';

// Electron 的硬件加速功能，在 win7 或者 Linux 系统上，容易出现黑屏或者卡死。禁用当前应用程序的硬件加速。
app.disableHardwareAcceleration();

// 为Windows 10+通知设置应用程序名称
if (process.platform === "win32") app.setAppUserModelId(app.getName());

// 限制只启动一个应用，防止多个实例
if (!app.requestSingleInstanceLock()) {
  app.quit();
  process.exit(0);
}

async function initWindow() {
  win = new BrowserWindow({
    width: 1200,
    minWidth: 900,
    height: 750,
    frame: false,
    resizable: true,
    // transparent: true,
    transparent: true,
    backgroundColor: '#00000000',
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      webviewTag: true,
    }
  })
  // win.webContents.openDevTools();
  mysql.con.connect();
  // mysql.initCreateTable();
  capture(win);
  if (process.env.npm_lifecycle_event == 'dev') {
    win.loadURL('http://localhost:3009/');
  } else {
    win.loadFile(path.join(__dirname, "./index.html"))
  }
}

app.whenReady().then(initWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
    mysql.con.end()
    process.exit()
  }
})


ipcMain.handle('open-dev', (e, data) => {
  win.webContents.openDevTools();
})

ipcMain.handle('words', (e, data) => {
  return new Promise((resolve, reject) => {
    if (data.value) {
      mysql.con.query(data.sql, data.value, (err, res) => {
        resolve(res);
        reject(err)
      })
    } else {
      mysql.con.query(data.sql, (err, res) => {
        resolve(res);
        reject(err)
      })
    }
  })
})

ipcMain.handle('sql', (e, data) => {
  return new Promise((resolve, reject) => {
    let sql = `select * from ${data.table} where 1=1 ${data.where || ''} limit ${data.size} offset ${data.page && ((data.page - 1) * data.size) || 0}`;
    let st = `select count(id) as total from ${data.table} where 1=1 ${data.where || ''}`;
    let result = {
      total: "",
      list: []
    }
    mysql.con.query(sql, (err, res: any) => {
      result.list = res;
      mysql.con.query(st, (err, res: any) => {
        result.total = res[0].total;
        resolve(result)
        reject(err)
      })
    })
  })
})

ipcMain.handle('dict', (e, data) => {
  return fetch(`https://dict.youdao.com/jsonapi?q=${data.word}`).then(res => {
    return res.json()
  })
})


ipcMain.handle('pinyin', (e, data) => {
  return fetch(`https://hanyuapp.baidu.com/dictapp/word/detail_getworddetail?wd=%E5%93%97&client=pc&lesson_from=xiaodu&smp_names=wordNewData1`).then(res => {
    return res.json()
  })
})


ipcMain.handle('deepseek', (e, data) => {
  return deepseek(data.list, data.isJson)
})

ipcMain.handle('setFileUrl', async (e, data) => {
  const result = await dialog.showOpenDialog({
    properties: ['openDirectory']
  });

  if (!result.canceled) {
    const folderPath = result.filePaths[0];
    return folderPath;
  }
})

ipcMain.handle('cropAudio', (e, data) => {
  ffmpeg.setFfmpegPath(ffmpegPath)
  let command = ffmpeg();
})

ipcMain.handle('win-bar', async (event, data) => {
  if (win) {
    if (data == 'close') {
      win.close();
      process.exit()
    } else if (data == 'max') {
      if (win.isMaximized()) {
        win.unmaximize();
      } else {
        win.maximize();
      }
    } else if (data == 'min') {
      if (win.isMinimized()) {
        win.restore();
      } else {
        win.minimize();
      }
    }
  }
})